package com.snow.useviewdemo;

import android.app.Activity;
import android.content.Context;
import android.os.Environment;
import android.util.Log;

import androidx.multidex.MultiDex;
import androidx.multidex.MultiDexApplication;

import com.snow.common.CommonLibConstant;
import com.snow.crash.CrashHandler;
import com.snow.playl.PlayAPPConstant;
import com.snow.useviewdemo.function2.hindicon.OpenHighActivity;
import com.snow.useviewdemo.util.LogUtils;
import com.snow.useviewdemo.util.PreferencesDataStoreUtils;
import com.snow.useviewdemo.util.lifecyler.SNLifecycleHelper;
import com.snow.useviewdemo.util.lifecyler.SNLifecycleListener;
import com.tencent.smtt.sdk.QbSdk;

import me.jessyan.autosize.AutoSizeConfig;
import me.jessyan.autosize.unit.Subunits;

/**
 * author : zyt
 * e-mail : 632105276@qq.com
 * date   : 2019/5/13
 * desc   :
 */
public class APPApplication extends MultiDexApplication {
    public String LOCAL_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()
            + "/Android/data/";
    private static APPApplication applicationContext;

    @Override
    public void onCreate() {
        super.onCreate();
        applicationContext = this;
        LOCAL_PATH = LOCAL_PATH + this.getPackageName() + "/";
        init();
    }

    public static APPApplication getInstance() {
        return applicationContext;
    }

    private void init() {
        //今日头条适配配置
        AutoSizeConfig.getInstance().getUnitsManager()
                .setSupportDP(false)
                .setSupportSP(false)
                .setSupportSubunits(Subunits.NONE);
        //common依赖库的相关初始化
        CommonLibConstant.init()
                .setAppContext(this)
                .setNoNetWorkRemind("无网络")
                //存储权限别忘了
                .setSharedPreferencesName("userview_db")
//                .setCrashSavePath(LOCAL_PATH)
                .setExternalNetworkIP();//要设置这个必有连网权限
        //崩溃日志辅助工具类
        CrashHandler.getInstance()
                .init(this, LOCAL_PATH)//这个必须调用
                .setIsShowCrashActivity(true)//崩溃后是否跳转崩溃日志activity 默认不跳
                .setIsSaveCrashLog(true);//崩溃后是否将日志到指定路径 默认不存
        LogUtils.setDebug(BuildConfig.DEBUG);
        PlayAPPConstant.init().setPlayContext(this);
        initLifecyler();
        //下载x5内核，可以不需要，因为会共用其他软件的x5内核，比如微信、QQ等
        QbSdk.setDownloadWithoutWifi(true);

        PreferencesDataStoreUtils.Companion.getInstance().init(this);

        QbSdk.initX5Environment(this, new QbSdk.PreInitCallback() {
            @Override
            public void onCoreInitFinished() {
                Log.e("snow", "========onCoreInitFinished===");
            }

            @Override
            public void onViewInitFinished(boolean b) {
                //加载x5内核成功返回值为true，否则返回false，加载失败会调用系统的webview
                Log.e("snow", "x5初始化结果====" + b);
            }
        });
    }

    private void initLifecyler() {
//        SNLifecycleHelper.setWhiteList(new ArrayList<>());
        SNLifecycleHelper.registerActivityLifecycle(this, new SNLifecycleListener() {
            @Override
            public void onBecameForeground(Activity activity) {
                if (activity == null) {
                    return;
                }
                String simpleName = activity.getClass().getSimpleName();
                Log.d("appForeground", "----->>> 前台=====" + simpleName);
            }

            @Override
            public void onBecameBackground(Activity activity) {
                if (activity == null) {
                    return;
                }
                String simpleName = activity.getClass().getSimpleName();
                Log.d("appForeground", "----->>> 后台=====" + simpleName);
            }
        });
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}
